const router = require('koa-router')()
const dbs = require('../../mysql/index').isconnect()

router.post('/add', async ctx => {
  let {
    name,
    material,
    face,
    msg,
    packing,
    other,
    type,
    img,
    imgs,
  } = ctx.request.body

  console.log('allmsg', ctx.request.body, String(ctx.request.body))

  if (Object.keys(ctx.request.body).length == 0) {
    console.log('commmm')
    ctx.body = { code: 10000, result: '数据格式错误' }
  } else if (isNaN(type)) {
    ctx.body = { code: 10000, result: '类型应该是数值类型' }
  } else {
    let res = await dbs.insert(
      'good',
      `('${name}','${material}','${face}','${msg}','${packing}','${other}',${type},'${img}','${imgs}')`,
      'backstage'
    )
    if (res.code == 1) {
      ctx.body = { code: 200, result: '请求成功' }
    } else {
      ctx.body = { code: 10000, result: '数据失败' }
    }
  }
})

function isLegitimate() {}

module.exports = router.routes()
